/*
 * Open LiveReplay Server - Live replay server for Supreme Commander
 * 
 * Copyright (C) 2009, 2009 Open LiveReplay Server Project
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details:
 * http://www.gnu.org/copyleft/lesser.html#TOC3
 */
package net.galacticwar.lua;

/**
 * 
 * An LUA pair is one element of an LUA table
 * It may be composed of two objects (key, value)
 * 
 * @author Yoann Meste (aka Mancer)
 *
 */
public class LuaPair {
	private Object first;
	private Object second;
	
	private boolean hasEqual;
	
	public LuaPair() {
		
	}
	
	public void push(Object o){
		if (first==null) {
			first = o;
		}
		else if (hasEqual && second == null) {
			second = o;
		}
		else{
			throw new RuntimeException("Impossible to push " + o );
		}
	}
	
	public void setEqual(boolean hasEqual) {
		this.hasEqual = hasEqual;
	}
	
	
	public Object getKey() {
		if (second!=null) {
			return first;
		}
		return null;
	}
	
	public Object getValue() {
		if (second !=null) {
			return second;
		}
		return first;
		
	}
	
	public boolean isEmpty(){
		if (first==null && second ==null) {
			return true;
		}
		return false;
	}
	
	@Override
	public String toString() {
		if (hasEqual) {
			return first + "=" + second;
		}
		return first.toString();
		
	}
}
